#!/bin/bash

yum install -y mysql-server zookeeper kafka redis

# mysql
if systemctl is-active --quiet mysqld; then
	echo "✅ mysqld is actived"
else
	systemctl start mysqld
fi

DB_NAME="HUAWEI_RA"
if mysql -u root -proot -e "SHOW DATABASES;" | grep -q "^$DB_NAME$"; then
    	echo "✅ database $DB_NAME existed"
else
	# mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';"
	mysql -u root -proot -e "FLUSH PRIVILEGES;"
	mysql -u root -proot -e "CREATE DATABASE $DB_NAME CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
	mysql -u root -proot -e "CREATE USER 'hisec'@'%' IDENTIFIED BY 'hisec';"
	mysql -u root -proot -e "GRANT ALL PRIVILEGES ON $DB_NAME.* TO 'hisec'@'%';"
	mysql -u root -proot -e "FLUSH PRIVILEGES;"
fi


# redis
REDIS_CONF="/etc/redis.conf"
NEED_RESTART=false

if grep -qE '^bind 0\.0\.0\.0([[:space:]]|$)' "$REDIS_CONF" && \
   grep -qE '^protected-mode no([[:space:]]|$)' "$REDIS_CONF"; then
   echo "redis config is right"
else
   sed -i '/^bind /d; /^protected-mode /d' "$REDIS_CONF"
   echo -e "bind 0.0.0.0\nprotected-mode no" >> "$REDIS_CONF"
   echo "🔧 redis update config：- bind 0.0.0.0 - protected-mode no"
   NEED_RESTART=true
fi

if $NEED_RESTART; then
   systemctl restart redis
else
   echo "✅ redis deploy success"
fi

# zookeeper

ZOOKEEPER_CONF="/opt/zookeeper/conf/zoo.cfg"  
NEED_RESTART=false

if grep -qE '^admin\.enableServer=false$' "$ZOOKEEPER_CONF"; then
    echo "zookeeper config is right"
else
    sed -i '/^admin\.enableServer=/d' "$ZOOKEEPER_CONF"
    echo "admin.enableServer=false" >> "$ZOOKEEPER_CONF"
    echo "🔧 zookeeper update config:admin.enableServer=false"
    NEED_RESTART=true
fi

if $NEED_RESTART; then
    systemctl restart zookeeper
else
    echo "✅ zookeeper deploy success"
fi


# kafka
KAFKA_SERVICE_FILE="/etc/systemd/system/kafka.service"
KAFKA_CONFIG="/opt/kafka/config/server.properties"
KAFKA_PORT=29092
LOCAL_IP=$(hostname -I | awk '{print $1}')
NEED_RESTART=false

if [ -f "$KAFKA_SERVICE_FILE" ] &&
   grep -q "After=zookeeper.service" "$KAFKA_SERVICE_FILE"; then
    echo "kafka service config is right"
else
    echo "deploy systemd service..."
    cat > "$KAFKA_SERVICE_FILE" <<EOF
[Unit]
Description=Apache Kafka Server
After=zookeeper.service

[Service]
Type=simple
User=kafka
ExecStart=/opt/kafka/bin/kafka-server-start.sh $KAFKA_CONFIG
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    echo -e "✅ Kafka systemd deployd success${NC}"
fi


if grep -qE '^listeners=PLAINTEXT://:29092$' "$KAFKA_CONFIG" && \
   grep -qE "^advertised.listeners=PLAINTEXT://${LOCAL_IP}:29092$" "$KAFKA_CONFIG"; then
   echo "kafka config is right"
else
   # 删除旧配置并写入新配置
   sed -i '/^listeners=/d; /^advertised.listeners=/d' "$KAFKA_CONFIG"
   echo -e "listeners=PLAINTEXT://:29092\nadvertised.listeners=PLAINTEXT://${LOCAL_IP}:29092" >> "$KAFKA_CONFIG"
   echo "🔧 kafka update config："
   echo "   - listeners=PLAINTEXT://:29092"
   echo "   - advertised.listeners=PLAINTEXT://${LOCAL_IP}:29092"
   NEED_RESTART=true
fi

# 重启判断
if $NEED_RESTART; then
	systemctl restart kafka
else
	echo "✅ kafka deploy success"
fi
